import { defineStore } from 'pinia'
import defaultSettings from '@/default-settings.ts'
import { useDark, useToggle } from '@vueuse/core'






const isDark = useDark()
const toggleDark = useToggle(isDark)
const { sideTheme, showSettings, topNav, tagsView, tagsIcon, fixedHeader, sidebarLogo, dynamicTitle, footerVisible, footerContent } = defaultSettings
const storageSetting= JSON.parse(localStorage.getItem('layout-setting') || '{}')
const useSettingsStore = defineStore(
  'settings',
  {
    state: () => ({
      title: '',
      theme: storageSetting.theme || '#409EFF',
      sideTheme: storageSetting.sideTheme || sideTheme,
      showSettings: showSettings,
      topNav: storageSetting.topNav === undefined ? topNav : storageSetting.topNav,
      tagsView: storageSetting.tagsView === undefined ? tagsView : storageSetting.tagsView,
      tagsIcon: storageSetting.tagsIcon === undefined ? tagsIcon : storageSetting.tagsIcon,
      fixedHeader: storageSetting.fixedHeader === undefined ? fixedHeader : storageSetting.fixedHeader,
      sidebarLogo: storageSetting.sidebarLogo === undefined ? sidebarLogo : storageSetting.sidebarLogo,
      dynamicTitle: storageSetting.dynamicTitle === undefined ? dynamicTitle : storageSetting.dynamicTitle,
      footerVisible: storageSetting.footerVisible === undefined ? footerVisible : storageSetting.footerVisible,
      footerContent: footerContent,
      isDark: isDark.value
    }),
    actions: {
      // 修改布局设置未使用，\src\layout\components\Settings\index.vue的每一个配置直接关联了state的属性
      // changeSetting(data:any) {
      //   const { key, value } = data
      //   if (this.hasOwnProperty(key)) {
      //     this[key] = value
      //   }
      // },


      // 设置网页标题
      setTitle(title:string) {
        this.title = title
          /**
           * 动态修改标题
           */
          if (this.dynamicTitle) {
            document.title = this.title + ' - ' + defaultSettings.title
          } else {
            document.title = defaultSettings.title
          }

      },
      // 切换暗黑模式
      toggleTheme() {
        this.isDark = !this.isDark
        toggleDark()
      }
    }
  })

export default useSettingsStore
